//index.js 当前商户的所有顾问的列表页
import SessionUtil from '../../../data/session/session'
import SellingServerUtil from '../../../data/sellingServer';
const Common = require('../../../utils/common.js');

Page({
    data: {
        consultantList: {},
        pageNo: 20,
    },
    onLoad: function (options) {
        SessionUtil.getInstance().then(session => {
            wx.setNavigationBarTitle({
                title: '顾问列表'
            })
            this.temList();
        })
    },
    temList: function () {
        var page = this;
        var pageNo = page.data.pageNo;
        SellingServerUtil.getAccessibleServers(true).then(servers => {
            let consultantList = []
            servers.slice(0, pageNo).forEach(function(server){
                let vCard = server.getTheVCardSync()
                consultantList.push({
                    salesmanId: server.salesmanId,
                    vCardId: vCard.id,
                    name: vCard.name,
                    avatarUri: vCard.avatarUri,
                    introduction: vCard.introduction
                })
            });
            page.setData({
                consultantList: consultantList
            })
            if (servers.length < pageNo - 20) {
                Common.Alert('提示', '没有更多了！');
            }
        })
    },
    onPullDownRefresh: function () {
        var page = this;
        page.setData({
            consultantList: {},
            pageNo: 20,
        });
        page.temList();
        wx.stopPullDownRefresh();
    },
    onReachBottom: function () {
        var page = this;
        var pageNo = page.data.pageNo;
        page.setData({
            pageNo: pageNo + 20,
        });
        page.temList();
    },
    searchName: function (e) {
        var page=this;
        var uname = e.detail.value;
        SellingServerUtil.getAccessibleServers(true).then(servers => {
            let locationServers = servers
            if (uname) {
                locationServers = []
                for (var i = 0; i < servers.length; i++) {
                    let vCard = servers[i].getTheVCardSync()
                    if (vCard.name.indexOf(uname) >= 0) {
                        locationServers.push(servers[i]);
                    }
                }
            }
            let consultantList = []
            locationServers.forEach(function(server){
                let vCard = server.getTheVCardSync()
                consultantList.push({
                    salesmanId: server.salesmanId,
                    vCardId: vCard.id,
                    name: vCard.name,
                    avatarUri: vCard.avatarUri,
                    introduction: vCard.introduction
                })
            });
            page.setData({
                consultantList: consultantList,
            })
            
        })
    },
    selectServer: function (e) {
        var page = this;
        var id = e.currentTarget.id;
        var consultantList = page.data.consultantList;
        for (var i = 0; i < consultantList.length; i++) {
            if (consultantList[i].account_id == id) {
                wx.setStorageSync('consultant_storge', consultantList[i]);
                if (wx.getStorageSync('user_id') < 0) {
                    var data = {
                        consultants: consultantList[i],
                    }
                    //页面跳转处理器
                    Common.PageJump('pages/register/index', data);
                } else {
                    Common.my_request({
                        url: 'AppMaster/Account/selectNewConsultant',
                        data: {
                            consultant_id: id
                        },
                        success: function (res) {
                            if (res.data.result == 0) {
                                wx.showToast({
                                    title: '顾问切换成功',
                                    duration: 2000
                                })
                                Common.when_user_change_clear_the_storage(function () {
                                    setTimeout(function () {
                                        wx.switchTab({
                                            url: '/pages/index/index'
                                        })
                                    }, 1000)
                                });
                            } else {
                                wx.showToast({
                                    title: res.data.message,
                                    image: '../../imges/fail.png',
                                    duration: 2000
                                })
                                setTimeout(function () {
                                    wx.switchTab({
                                        url: '/pages/index/index'
                                    })
                                }, 2000)
                            }
                        }
                    });
                }
            }
        }
    },
    showVCard: function (e) {
        var i = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/register/information1/index?salesmanVCardId=' + i + '&fromid=1',
        })
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },
})